
/**
 * 基类
 */
function Base() {}

/**
 * 设置按钮
 */
Base.prototype.setButton = function() {
	var base = this;
	
	var buttonAddClassName = 'buttonAdd' + this.className;
	var buttonModifyClassName = 'buttonModify' + this.className;
	var buttonDeleteClassName = 'buttonDelete' + this.className;
	var buttonListClassName = 'buttonList' + this.className;
	
	$('.buttonAddModifyDelete, .buttonList').append('<span class="hover"></span>').each(function() {
		var $span = $('> span.hover', this).css('opacity', 0);
		$(this).hover(function() {
			$span.stop().fadeTo(500, 1);
		}, function() {
			$span.stop().fadeTo(500, 0);
		})
		
		$(this).click(function() {
			if(this.className.indexOf(buttonAddClassName) != -1) {
				base.closeDivButton();
				base.showForm();
			}
			else if(this.className.indexOf(buttonModifyClassName) != -1) {
				if(!base.checkTreeIsSelected()) {
					return;
				}
				
				base.closeDivButton();
				base.showForm();
				base.loadForm();
			}
			else if(this.className.indexOf(buttonDeleteClassName) != -1) {
				if(!base.checkTreeIsSelected()) {
					return;
				}
				
				base.deleteById();
			}
			else if(this.className.indexOf(buttonListClassName) != -1) {
				if(!base.checkTreeIsSelected()) {
					return;
				}
				
				base.closeDivButton();
				base.showList();
			}
		})
	});
	
	$('.divButtonMaximize').click(function() {
		base.openDivButton();
	})
}

/**
 * 显示树形
 * @param {} params
 */
Base.prototype.showTree = function() {
	this.treePanel = new Ext.tree.TreePanel({
		root: {id: -1, text: this.treePanelRootText, expanded: true},
		dataUrl: '/szpxt/' + this.lowerClassName + '_getTree',
		border: false,
		renderTo: 'divTree',
		listeners: {
			scope: this,
			'click': function(node, event) {
				//父节点
				var textParentId = Ext.getCmp('parentId');
				var textParentName = Ext.getCmp('parentName');
				
				if(textParentId && textParentName) {
					textParentId.setValue(node.id);
					textParentName.setValue(node.text);
				}
				
				//相关节点
				var textRelationId = Ext.getCmp(this.lowerClassName + 'Id');
				var textRelationName = Ext.getCmp(this.lowerClassName + 'Name');
				
				if(textRelationId && textRelationName) {
					textRelationId.setValue(node.id);
					textRelationName.setValue(node.text);
				}
				
				//刷新列表
				if(this.relation.gridPanel) {
					this.relation.gridPanel.store.reload({params: {relationId: node.id}});
				}
			}
		}
	})
}

/**
 * 显示表格
 * @param {} params
 */
Base.prototype.showForm = function() {
	$('#divForm').empty();
	var base = this;
	
	base.formPanel = new Ext.form.FormPanel({
		id: 'form',
		title: base.formPanelTitle,
		frame: true,
		width: 400,
		labelWidth: 80,
		renderTo: 'divForm',
		items: base.formPanelItems,
		buttonAlign: 'center',
		buttons: [
			{text: '保存', id: 'btFormSave', handler: function() {
				Ext.getCmp('btFormSave').disable();
				Ext.getCmp('btFormReset').disable();
				
				base.formPanel.getForm().submit({
					url: '/szpxt/' + base.lowerClassName + '_saveOrUpdate',
					success: function(form, action) {
						var responseText = Ext.decode(action.responseText);
						
						if(base.hideButtonAfterSave) {
							Ext.getCmp('btFormSave').hide();
							Ext.getCmp('btFormReset').hide();
						}
						else {
							Ext.getCmp('btFormSave').enable();
							Ext.getCmp('btFormReset').enable();
						}
						
						if(base.treePanel) {
							base.treePanel.root.reload();
						}
						else if(base.gridPanel) {
							base.gridPanel.store.reload();
						}
						
						base.showInfoMessage('保存数据成功！');
					},
					failure: function(form, action) {
						Ext.getCmp('btFormSave').enable();
						Ext.getCmp('btFormReset').enable();
						
						switch(action.failureType) {
							case(Ext.form.Action.CONNECT_FAILURE):
								base.showErrorMessage('服务器连接失败！');
								break;
							case(Ext.form.Action.SERVER_INVALID):
								base.showErrorMessage('服务器检验数据失败！');
								break;
						}
					}
				})
			}},
			{text: '清空', id: 'btFormReset', scope: this, handler: function() {
				base.formPanel.getForm().reset();
			}}
			
		]
	})
	
	$('#divForm').show('slow');
}

/** 这个暂时不用
Base.prototype.showImages = function() {
	var store = new Ext.data.JsonStore({
		url: '/szpxt/staff_listByParams',
		root: 'data',
		fields: ['id', 'name', 'companyId', 'companyName', 'eMail', 'imageName'],
		autoLoad: true
	});
	
	store.filterBy(function(record, id) {
		if(record.data.imageName) {return true;}
		else {return false;}
	})
	
	var tpl = new Ext.XTemplate(
	    '<tpl for=".">',
	        '<div class="thumb"><img src="/szpxt/staff_displayImage?imageName={imageName}" title="{name}"></div>',
	    '</tpl>');
	    
	var panel = new Ext.Panel({
    	frame:true,
    	height: 300,
   	 	width:255,
    	layout:'fit',
    	autoScroll: true,

    items: new Ext.DataView({
        store: store,
        tpl: tpl,
        autoHeight:true,
        multiSelect: true,
        overClass:'x-view-over',
        itemSelector:'div.thumb-wrap',
    })
});
panel.render('divList');

}
*/

/**
 * 显示列表
 * @param {} params
 */
Base.prototype.showList = function() {
	$('#divList').empty();
	
	var relationId = '';
	if(this.relation) {
		relationId = this.relation.treePanel.getSelectionModel().getSelectedNode().id;
	}
	
	this.gridPanel = new Ext.grid.GridPanel({
		store: new Ext.data.JsonStore({
			url: '/szpxt/' + this.lowerClassName + '_listByParams',
			baseParams: {relationId: relationId},
			autoLoad: true,
			root: 'data',
			fields: this.gridFields
		}),
		columns: this.gridColumns,
		width: this.gridWidth || 680,
		height: this.gridHeight || 300,
		renderTo: 'divList',
		listeners: this.gridListeners
	});
	
	$('#divList').show('slow');
}

/**
 * 关闭DIV按钮
 */
Base.prototype.closeDivButton = function() {
	$('#divButton').hide('slow');
	
	$('.errorMessage').hide('slow');
	$('.infoMessage').hide('slow');
	
	$('.divButtonMaximize').show('slow');
}

/**
 * 打开DIV按钮
 */
Base.prototype.openDivButton = function() {
	$('.divButtonMaximize').hide('slow');
	
	$('.errorMessage').hide('slow');
	$('.infoMessage').hide('slow');
	
	//因为打开按钮DIV，所以要关闭的DIV
	this.hideByOpenDivButton();
	
	$('#divButton').show('slow');
}

/**
 * 显示错误信息
 * @param {} message
 */
Base.prototype.showErrorMessage = function(message) {
	$('.errorMessage').text('');
	
	$('.errorMessage').show();
	$('.errorMessage').append(message + '<img id="imageMessage" src="/szpxt/images/common/cancel.png"/>');
	
	$('#imageMessage').click(function() {
		$('.errorMessage').text('');
		$('.errorMessage').hide('slow');
	})
}

/**
 * 显示提示信息
 * @param {} message
 */
Base.prototype.showInfoMessage = function(message) {
	$('.infoMessage').text('');
	
	$('.infoMessage').show();
	$('.infoMessage').append(message + '<img id="imageMessage" src="/szpxt/images/common/cancel.png"/>');
	
	$('#imageMessage').click(function() {
		$('.infoMessage').text('');
		$('.infoMessage').hide('slow');
	});
}

/**
 * 检查树形结构是否被选择
 * @return {Boolean}
 */
Base.prototype.checkTreeIsSelected = function() {
	if(this.treePanel) {
		var selectedNode = this.treePanel.getSelectionModel().getSelectedNode();
		if(!selectedNode) {
			this.showErrorMessage('必须选择树形结构的一个节点');
			return false
		}
	}
	else if(this.relation && this.relation.treePanel) {
		var selectedNode = this.relation.treePanel.getSelectionModel().getSelectedNode();
		if(!selectedNode) {
			this.showErrorMessage('必须选择树形结构的一个节点');
			return false
		}
	}
	else if(this.gridPanel) {
		var selectedNode = this.gridPanel.getSelectionModel().getSelected();
		if(!selectedNode) {
			this.showErrorMessage('必须选择表格的一行');
			return false;
		}
	}
	
	return true;
}

/**
 * 载入表格数据
 */
Base.prototype.loadForm = function(id) {
	if(!id) {
		if(this.treePanel) {
			id = this.treePanel.getSelectionModel().getSelectedNode().id;
		}
		else if(this.gridPanel) {
			id = this.gridPanel.getSelectionModel().getSelected().id
		}
	}
	
	if(this.formPanel) {
		this.formPanel.getForm().load({
			url: 'szpxt/' + this.lowerClassName + '_getById',
			params: {id: id},
			scope: this,
			failure: function(form, action) {
				this.showErroMessage('服务器连接失败！');
			}
		});
	}
}

/**
 * 根据ID删除实体
 */
Base.prototype.deleteById = function(id) {
	if(!id) {
		if(this.treePanel) {
			id = this.treePanel.getSelectionModel().getSelectedNode().id;
		}
		else if(this.gridPanel) {
			id = this.gridPanel.getSelectionModel().getSelected().id;
		}
	}
	
	Ext.Ajax.request({
		url: '/szpxt/' + this.lowerClassName + '_deleteById',
		params: {id: id},
		scope: this,
		success: function(response, opts) {
			var responseText = Ext.decode(response.responseText);
			if(responseText.success) {
				if(this.treePanel) {
					this.treePanel.root.reload();
				}
				else if(this.gridPanel) {
					this.gridPanel.getStore().reload();
				}
				this.showInfoMessage('数据已经成功删除！');
			}
		},
		failure: function(response, opts) {
			this.showErrorMessage('服务器连接失败！');
		}
	});
}

/**
 * 通过表格显示人员基本信息
 * @param {} id
 */
Base.prototype.modifyByGrid = function(id) {
	$('#divList').hide('slow');
	
	$('.errorMessage').hide('slow');
	$('.infoMessage').hide('slow');
	
	this.showForm();
	this.loadForm(id);
}

Base.prototype.deleteByGrid = function(id) {
	var base = this;
	Ext.Msg.confirm('提示信息', '这个操作是不能够恢复，确定删除吗？', function(buttonId) {
		if(buttonId == 'yes') {
			base.deleteById(id);
		}
		return;
	})
}


